package com.citcc.business.service;

import com.citcc.business.bean.CdzAccountAmount;
import com.citcc.business.bean.vo.AccountAmountMyAmountVO;
import com.citcc.business.bean.vo.AccountAmountRechargeParamVO;
import com.citcc.business.mapper.CdzAccountAmountMapper;
import com.citcc.system.service.impl.SysDictDataServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Created by czh on 2019/5/17 14:21
 */
@Service
public class CdzAccountAmountService {

    @Autowired
    private CdzAccountAmountMapper accountAmountMapper;
    @Autowired
    private SysDictDataServiceImpl sysDictDataService;

    private static final DecimalFormat FORMAT = new DecimalFormat("#,##0.00");

    /**
     * 我的余额
     *
     * @param userId
     * @return
     */
    public AccountAmountMyAmountVO myAmount(String userId) {
        return Optional.ofNullable(accountAmountMapper.selectByUserId(userId)).map(s -> {
            AccountAmountMyAmountVO accountAmountMyAmountVO = new AccountAmountMyAmountVO();
            accountAmountMyAmountVO.setTotalAmount(s.getTotalAmount() == null ? "0.00" : FORMAT.format(s.getTotalAmount()));
            accountAmountMyAmountVO.setAvailableAmount(s.getAvailableAmount() == null ? "0.00" : FORMAT.format(s.getAvailableAmount()));
            accountAmountMyAmountVO.setFrozenAmount(s.getFrozenAmount() == null ? "0.00" : FORMAT.format(s.getFrozenAmount()));
            accountAmountMyAmountVO.setFrozenFlag(s.getFrozenFlag() == null ? null : Integer.valueOf(s.getFrozenFlag()));
            return accountAmountMyAmountVO;
        }).orElse(new AccountAmountMyAmountVO());
    }

    /**
     * 根据用户id查询账户金额对象
     *
     * @param userId
     * @return
     */
    public CdzAccountAmount getAccountAmount(String userId) {
        return accountAmountMapper.selectByUserId(userId);
    }

    /**
     * 更新数据
     *
     * @param cdzAccountAmount
     * @return
     */
    public int update(CdzAccountAmount cdzAccountAmount) {
        return accountAmountMapper.updateByUserIdSelective(cdzAccountAmount);
    }

    /**
     * 充值可选参数
     *
     * @param userId
     * @return
     */
    public AccountAmountRechargeParamVO rechargeParam(String userId) {

        AccountAmountRechargeParamVO amountRechargeParamVO = new AccountAmountRechargeParamVO();
        Optional.ofNullable(accountAmountMapper.selectByUserId(userId)).ifPresent(s -> {
            amountRechargeParamVO.setCurrentBalance(FORMAT.format(s.getAvailableAmount()));
        });

        Optional.ofNullable(sysDictDataService.selectDictDataByType("recharge_param")).ifPresent(result -> amountRechargeParamVO.setParamList(result.stream().map(s -> {
            AccountAmountRechargeParamVO.RechargeParamObj rechargeParamObj = new AccountAmountRechargeParamVO.RechargeParamObj();
            rechargeParamObj.setAmountNum(s.getDictValue());
            rechargeParamObj.setAmountName(s.getDictLabel());
            rechargeParamObj.setOrderBy(s.getDictSort());
            return rechargeParamObj;
        }).sorted(Comparator.comparing(AccountAmountRechargeParamVO.RechargeParamObj::getOrderBy)).collect(Collectors.toList())));

        return amountRechargeParamVO;
    }
}